//
//  TranscodingView.m
//  ebook
//
//  Created by mt on 16/10/9.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "TranscodingView.h"
#import "ReaderGlobalModel.h"

#define kAlertViewWidth 260
#define kAlertViewHeight 326
#define kTitleHeight 50      //title字体高度
#define kTitleFontSize 18    //title字体大小
#define kspaceV 20           //间隔高度
#define kContentLabelFontSize 16 //内容字体大小


@interface TranscodingView ()<UIGestureRecognizerDelegate>

@end

@implementation TranscodingView

- (instancetype)init {
    
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self = [super init]) {
        
    }
    return self;
}

- (void)show {
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnView:)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake((MTScreenW-kAlertViewWidth)/2, (MTScreenH-kAlertViewHeight)/2, kAlertViewWidth, kAlertViewHeight)];
    alertView.backgroundColor = MTColor(227, 229, 230, 1);
    alertView.layer.cornerRadius = 5;
    alertView.layer.masksToBounds = YES;
    [self addSubview:alertView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, alertView.width, kTitleHeight)];
    titleLabel.font = [UIFont systemFontOfSize:kTitleFontSize];
    titleLabel.text = @"转码声明";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = MTConfigColor(@"MTNavigationBar_background_Color");
    titleLabel.textColor = [UIColor whiteColor];
    [alertView addSubview:titleLabel];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:titleLabel.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(0, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = titleLabel.bounds;
    maskLayer.path = maskPath.CGPath;
    titleLabel.layer.mask = maskLayer;
    
    NSString *app_Name = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    NSString *text = [NSString stringWithFormat:@"　　%@已经对原页面通过转码技术转码，但未对源网站内容进行任何实质性的修改、编辑和存储。若页面存在问题，您可以去原网站阅读。", app_Name];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(titleLabel.frame)+kspaceV, alertView.width-10*2, 140)];
    contentLabel.font = [UIFont systemFontOfSize:kContentLabelFontSize];
    contentLabel.numberOfLines = 0;
    contentLabel.textColor = [UIColor darkGrayColor];
    contentLabel.text = text;
    contentLabel.textAlignment = NSTextAlignmentCenter;
    [alertView addSubview:contentLabel];
   
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentLabel.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:10];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentLabel.text length])];
    contentLabel.attributedText = attributedString;
    
    UIButton *stateBtn1 = [self buttonFrame:CGRectMake(0, CGRectGetMaxY(contentLabel.frame)+kspaceV, alertView.width, 45) title:@"查看服务声明" titleColor:[UIColor darkGrayColor] fontSize:18 btnTag:0 hasUnderlined:YES];
    [alertView addSubview:stateBtn1];
    
    UIButton *stateBtn2 = [self buttonFrame:CGRectMake(0, CGRectGetMaxY(stateBtn1.frame), stateBtn1.width, stateBtn1.height) title:@"查看原网页" titleColor:[UIColor darkGrayColor] fontSize:18 btnTag:1 hasUnderlined:YES];
    [alertView addSubview:stateBtn2];
    
    //关闭按钮
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *closeImg = [UIImage imageNamed:@"updateColse"];
    closeBtn.frame = CGRectMake(CGRectGetMaxX(alertView.frame)-2*closeImg.size.width/3, CGRectGetMinY(alertView.frame)-closeImg.size.height/3, closeImg.size.width, closeImg.size.height);
    [closeBtn setImage:closeImg forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    closeBtn.tag=3;
    [self addSubview:closeBtn];
}

- (NSAttributedString *)getAttributeText:(NSString *)text textColor:(UIColor *)color{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSForegroundColorAttributeName value:color range:strRange];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:strRange];
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    return str;
}

- (UIButton *)buttonFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)titleColor fontSize:(CGFloat)fontSize btnTag:(NSInteger)tag hasUnderlined:(BOOL)has {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.tag = tag;
    [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    if (has) {
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, btn.width-30, 0.5)];
        line.backgroundColor = MTColor(197, 199, 200, 1);
        [btn addSubview:line];
    }
    return btn;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    CGFloat minX = (MTScreenW-kAlertViewWidth)/2, maxX = MTScreenW-(MTScreenW-kAlertViewWidth)/2;
    CGFloat minY = (MTScreenH-kAlertViewHeight)/2, maxY = MTScreenH-(MTScreenH-kAlertViewHeight)/2;
    
    CGPoint point = [touch locationInView:self];
    
    if (!((point.x>minX&&point.x<maxX) && (point.y>minY && point.y<maxY))) {
        return YES;
    }
    
    return NO;
}

- (void)buttonClick:(UIButton *)btn {

    if ([self.delegate respondsToSelector:@selector(transcodingView:clickedButtonAtIndex:)]) {
        [self.delegate transcodingView:self clickedButtonAtIndex:btn.tag];
    }
    [self removeFromSuperview];
}

- (void)tapOnView:(UITapGestureRecognizer *)tap {
    
    [self removeFromSuperview];
}

@end
